package com.work.straighttalk.presenter;


import android.net.Uri;

import com.work.straighttalk.StraightTalkPlayer;
import com.work.straighttalk.bean.StraightTalkMessage;
import com.work.straighttalk.model.ChatMImpl;
import com.work.straighttalk.model.i.ChatMI;
import com.work.straighttalk.model.i.SeekBarProgressListener;
import com.work.straighttalk.presenter.i.ChatPI;
import com.work.straighttalk.presenter.i.OnIMSendListener;
import com.work.straighttalk.presenter.i.OnLoadFinishedListener;
import com.work.straighttalk.presenter.i.SeekBarMaxListener;
import com.work.straighttalk.ui.i.ChatVI;

import java.util.List;


/**
 * Created by dllo on 16/3/4.
 */
public class ChatPImpl implements ChatPI, OnLoadFinishedListener<List<StraightTalkMessage>>, OnIMSendListener, SeekBarProgressListener, SeekBarMaxListener {
    private ChatVI<StraightTalkMessage> chatVI;
    private ChatMI chatMI;

    /**
     * 信息 发送关键 id 在构造方法传入
     *
     * @param chatVI
     */
    public ChatPImpl(ChatVI chatVI) {
        this.chatVI = chatVI;
        chatMI = new ChatMImpl(chatVI.getID());
    }

    @Override
    public void loadData() {
        chatMI.queryMessage(this);
    }

    /**
     * 当点击发送消息 通过createMessage方法 创建EMMessage
     *
     * @param content 发送信息的内容
     */
    @Override
    public void sendTextMessage(String content) {
        chatVI.setEditTextNull();
        chatMI.sendTextMessage(content, this);
    }

    /**
     * 开始录音
     */
    @Override
    public void recordVoice() {
        chatMI.recordVoice();
    }

    @Override
    public void destory() {
        chatVI = null;
    }

    @Override
    public void startPlayer(final StraightTalkMessage emMessage) {
        chatMI.playVoice(emMessage, this, this, new StraightTalkPlayer.PlayerCallBack() {
            @Override
            public void onStop() {
                chatVI.showSeekBarProgress(emMessage.getEmMessage().getMsgId(), 0);
            }
        });
    }

    @Override
    public void seekTo(int progress) {
        chatMI.seekTo(progress);
    }

    @Override
    public void stopPlayer() {

    }

    @Override
    public void pausePlayer() {
        chatMI.pauseVoice();
    }

    /**
     * 用于控制播放语音
     */


    @Override
    public void discordVoice() {
        chatMI.discardVoice();
    }

    @Override
    public void sendPicByUri(Uri uri) {
        chatMI.sendPicByUri(uri, this);
    }

    @Override
    public void sendVoiceMessage() {
        chatMI.sendVoiceMessage(this);
    }


    /**
     * 查询数据库conversation成功后 加载数据显示在ui的回调
     *
     * @param emMessages
     */
    @Override
    public void onLoadSuccess(final List<StraightTalkMessage> emMessages) {
        chatVI.getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                chatVI.loadItems(emMessages);
            }
        });
    }


    @Override
    public void onLoadError() {

    }


    @Override
    public void afterSend(final List<StraightTalkMessage> message) {
        chatVI.getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                chatVI.loadItems(message);
                chatVI.toBottom();
            }
        });
    }

    @Override
    public void returnProgress(String msgID, int progress) {
        chatVI.showSeekBarProgress(msgID, progress);
    }

    @Override
    public void proofread(String msgID, int max) {
        if (max > 0) {
            chatVI.showSeekBarMax(msgID, max);
        }
    }


}
